package com.niuwa.p2p.service.impl.claimtransfer;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.DictConstant.PlanAuditStatus;
import com.niuwa.p2p.dao.business.CustClaimDao;
import com.niuwa.p2p.dao.business.InvestProdRecordDao;
import com.niuwa.p2p.dao.business.LoanClaimDao;
import com.niuwa.p2p.dao.business.PlanProductDao;
import com.niuwa.p2p.dao.claimtransfer.PlanTransLogDao;
import com.niuwa.p2p.dao.claimtransfer.PlanTransferDao;
import com.niuwa.p2p.dao.claimtransfer.TransferApplyDao;
import com.niuwa.p2p.dao.protocol.ProtocolDao;
import com.niuwa.p2p.entity.PageQuery;
import com.niuwa.p2p.entity.business.InvestProdRecord;
import com.niuwa.p2p.entity.claimtransfer.PlanTransLog;
import com.niuwa.p2p.entity.claimtransfer.PlanTransfer;
import com.niuwa.p2p.entity.claimtransfer.TransferApply;
import com.niuwa.p2p.entity.productTemplate.ProductProtocol;

//import static com.niuwa.p2p.server.manggoDB.exchange.ExchangeMongoDBHelper.*;

import com.niuwa.p2p.service.claimtransfer.PlanTransferService;
//import com.niuwa.p2p.service.exchange.IMatchService;
import com.niuwa.p2p.vo.business.InvestPlanProductVo;
import com.niuwa.p2p.vo.business.LoanClaimOriginVo;
import com.niuwa.p2p.vo.claimtransfer.PlanTransferVo;
@Service
public class PlanTransferServiceImpl implements PlanTransferService{
	@Autowired
	private PlanTransferDao planTransferDao;
	
	@Autowired
	private PlanProductDao planProductDao;
	@Autowired
	private ProtocolDao protocolDao;
	
	@Autowired
	private InvestProdRecordDao investProdRecordDao;
	
	@Autowired
	private LoanClaimDao loanClaimDao;
	
	@Autowired
	private TransferApplyDao transferApplyDao;
	
	@Autowired
	private PlanTransLogDao planTransLogDao;
	
	@Autowired
	private CustClaimDao custClaimDao;
	
	
//	@Autowired
//	private IMatchService matchService;

	@Override
	public PageBean<PlanTransferVo> searchPlanTransferPage(Map<String,Object> paramMap){
			assert paramMap.get(PageQuery.PAGEQUERY) != null;
			int totalCount = planTransferDao.queryCount(paramMap);
			PageBean<PlanTransferVo> page  = new PageBean<PlanTransferVo>(totalCount);
			// 当前页
			page.setPage(((PageQuery)paramMap.get(PageQuery.PAGEQUERY)).getPage());
			page.setRows(planTransferDao.pageQuery(paramMap));
			return page;
	}

	@Override
	public PageBean<TransferApply> searchPlanTransferApplyList(String recordNo){
		PageBean<TransferApply> page = new PageBean<TransferApply>(Integer.MAX_VALUE);//不分页
		page.setRows(transferApplyDao.selectPlanTransferList(recordNo));
		return page;
	}
	
	public InvestPlanProductVo viewPlanProDetail(String prodCode){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("productCode", prodCode);
		InvestPlanProductVo investPlanProductVo =  planProductDao.getInvestPlanBySelective(map);
		return investPlanProductVo;
	}


	@Override
	public List<ProductProtocol> getProductProtocol(String prodCode) {
		// TODO Auto-generated method stub
		return protocolDao.getProdRelativeProt(prodCode);
	}
	
	public BigDecimal getWaitMatchFund(){
		return investProdRecordDao.getWaitMatchFund()== null ? BigDecimal.ZERO : investProdRecordDao.getWaitMatchFund() ;
	}


	@Override
	public BigDecimal getWaitClaimMatchFund() {
		//return loanClaimDao.selectToAssignClaimAmout() == null ? BigDecimal.ZERO : loanClaimDao.selectToAssignClaimAmout();
		return transferApplyDao.getWaitTransferFund();
	}

	@Override
	public boolean saveAduitPass(String recordNo, Long userId) {
		PlanTransfer ptf = new PlanTransfer();
		ptf.setRecordNo(recordNo);
		ptf.setStatus(PlanAuditStatus.QUITTING.getCode());
		boolean up1 = planTransferDao.updateByPrimaryKeySelectiveByVersion(ptf) > 0?true:false;
		PlanTransLog ptl = new PlanTransLog();
		ptl.setCreateTime(new Date());
		ptl.setRecordCode(recordNo);
		ptl.setOperateType("PASSED");
		ptl.setOperator(userId);
		boolean in1 = planTransLogDao.insertSelective(ptl) > 0 ? true : false;
		//更新投资记录下的债权申请状态
		Map<String,String> map = new HashMap<String,String>();
		map.put("tranferStatus","NO_MATCH");
		map.put("recordNo", recordNo);
		transferApplyDao.updatePlanClaimByPlanTransferRecordCode(map);
		if(up1 && in1){
			/**
			 * 审核通过后将转让申请加入到申请表中
			 */
			
			InvestProdRecord investProdRecord=investProdRecordDao.selectByPrimaryKey(recordNo);
			List<TransferApply> tranferApply =  transferApplyDao.selectPlanTransferList(recordNo);
			
			if(tranferApply != null && tranferApply.size() > 0){
				Map<String,String> paramMap = new HashMap<String,String>();
				for(TransferApply tf : tranferApply){
					String creditNo=tf.getCreditNo();
					Long creditNoVersion=custClaimDao.selectCustClaimByCreditNo(creditNo).getVersion();
//					paramMap.put(USER_ID, String.valueOf(investProdRecord.getCustId()));
//					paramMap.put(ID, tf.getSerialNo());
//					paramMap.put(lOAN_AMOUNT, String.valueOf(tf.getTransferCapital()));
//					paramMap.put(VERSION,String.valueOf(tf.getVersion()));
//					paramMap.put(TR_PROJECT_NO, tf.getProjectCode());
//					paramMap.put(TR_RECORD_NO, recordNo);
//					paramMap.put(TR_CREDIT_NO, creditNo);
//					paramMap.put(TR_CREDIT_VERSION, creditNoVersion.toString());
//					paramMap.put(PRODUCT_CODE, tf.getProdCode());
//					paramMap.put(PRODUCT_NAME, tf.getProdName());
					//{user_id-债权持有人Id,_id-债权转让申请编号,loan_amount-债权转让金额,version-债权转让申请版本号,projectNo-债权转让的项目编号,
					// recordNo-债权转让者的投资编号,creditNo-债权转让的债权编号,creditVersion-债权转让的用户债权版本号,productCode-转让人的投资产品代码,
					// productName-转让人的投资产品名称}
//					matchService.doAddLoanTransfer(paramMap);
				}
			}else{
				System.out.println("未查找到投资转让计划对应的债权!");
			}
			return true;
		}
		return false;
	}

	@Override
	public boolean saveAduitReject(String recordNo, Long userId,String remark) {
		PlanTransfer ptf = new PlanTransfer();
		ptf.setRecordNo(recordNo);
		ptf.setStatus(PlanAuditStatus.NO_PASS.getCode());
		boolean up1 = planTransferDao.updateByPrimaryKeySelectiveByVersion(ptf) > 0?true:false;
		PlanTransLog ptl = new PlanTransLog();
		ptl.setCreateTime(new Date());
		ptl.setRecordCode(recordNo);
		ptl.setOperateType("REJECT");
		ptl.setOperator(userId);
		ptl.setRemark(remark);
		boolean in1 = planTransLogDao.insertSelective(ptl) > 0 ? true : false;
		//更新投资记录下的债权申请状态
		Map<String,String> map = new HashMap<String,String>();
		map.put("tranferStatus","NO_PASS");
		map.put("recordNo", recordNo);
		transferApplyDao.updatePlanClaimByPlanTransferRecordCode(map);
		if(up1 && in1)
			return true;
		return false;
	}

	@Override
	public boolean saveAduitReApply(String recordNo, Long userId) {
		PlanTransfer ptf = new PlanTransfer();
		ptf.setRecordNo(recordNo);
		ptf.setStatus(PlanAuditStatus.WAIT_AUDIT.getCode());
		boolean up1 = planTransferDao.updateByPrimaryKeySelectiveByVersion(ptf) > 0?true:false;
		PlanTransLog ptl = new PlanTransLog();
		ptl.setCreateTime(new Date());
		ptl.setRecordCode(recordNo);
		ptl.setOperateType("REAPPLY");
		ptl.setOperator(userId);
		boolean in1 = planTransLogDao.insertSelective(ptl) > 0 ? true : false;
		//更新投资记录下的债权申请状态
		Map<String,String> map = new HashMap<String,String>();
		map.put("tranferStatus","WAIT_AUDIT");
		map.put("recordNo", recordNo);
		transferApplyDao.updatePlanClaimByPlanTransferRecordCode(map);
		if(up1 && in1)
			return true;
		return false;
	}

	@Override
	public PlanTransLog getNewestAudit(String recordNo) {
		return planTransLogDao.getNewstAduit(recordNo);
	}
	

	public LoanClaimOriginVo getLoanClaimByProjectNo(String projectNo){
		return loanClaimDao.selectClaimLoanDetailByProjectNo(projectNo);
	}

	@Override
	public PlanTransfer getByRecordNo(String recordNo) {
		return planTransferDao.selectByPrimaryKey(recordNo);
	}

}
